package es.cazadoresmadridejos.beans.facade.event;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import es.cazadoresmadridejos.beans.manager.biggame.BigGameManager;
import es.cazadoresmadridejos.beans.manager.event.EventManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.model.BigGame;
import es.cazadoresmadridejos.model.Event;
import es.cazadoresmadridejos.model.Event.EventType;
import es.cazadoresmadridejos.model.Reason;

@Service
@RemoteProxy(name="eventFacade",creator=SpringCreator.class)
public class EventFacade {

	@Autowired private EventManager eventManager;
	@Autowired private BigGameManager bigGameManager;
	@Autowired(required=false) private UserPreferences userPreferences;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("d_M_yyyy");
	
	@RemoteMethod
	public void addEvent(String id, String type) throws ParseException{
//		String[] split = id.split("_");
//		String day = split[0];
//		String month = split[1];
//		String year = split[2];
		
		
		Event event = new Event();
		event.setDate(sdf.parse(id));
		Reason reason  = userPreferences.getReason();
		event.setReason(reason);
		event.setType(EventType.valueOf(type));
		eventManager.save(event);
	}
	@RemoteMethod
	public void deleteEvent(String id, String type) throws ParseException{
//		String[] split = id.split("_");
//		String day = split[0];
//		String month = split[1];
//		String year = split[2];
		
		Event event = new Event();
		event.setDate(sdf.parse(id));
		Reason reason  = userPreferences.getReason();
		event.setReason(reason);
		event.setType(EventType.valueOf(type));
		
		eventManager.deleteByExample(event);
	}
	@RemoteMethod
	public List<String> findEvents(String type) throws ParseException{
		EventType eventType = EventType.valueOf(type);
		return eventManager.findAllByType(eventType,userPreferences.getReason().getId());
	}
}
